<?php

namespace app\index\controller;

use think\Db;
use think\wxpay\example\JsApiPay;
use app\common\controller\Wechat;
use think\wxpay\lib\WxPayUnifiedOrder;
use think\wxpay\lib\WxPayConfig;
use think\wxpay\lib\WxPayApi;
use app\common\api\controller\Bonus;
use think\Validate;

class Store extends Home
{
    protected $beforeActionList = [
        'check_load' => ['except' => '']
    ];

    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 商家入驻
     */
    public function apply()
    {
        if (!request()->isPost()) {
            $this->apiError('非法操作！');
        }

        $uid = is_login();
        $data = $_POST;
        $rule = [
            'company_name|公司名称' => 'require',
            'organization_code|组织机构代码' => 'require',
            'store_mobile|联系电话' => 'require',
            'store_imgs|店铺照片' => 'require',
            'store_area|店铺所在区域' => 'require',
            'store_address|店铺详细地址' => 'require',
            'store_type|店铺类型' => 'require',
            'legal_area|法人所在区域' => 'require',
            'legal_name|法人姓名' => 'require',
            'legal_mobile|法人电话' => 'require',
            'business_license_img|公司营业执照' => 'require',
            'ID1|法人身份证正面照' => 'require',
            'ID2|法人身份证反面照' => 'require',
            'bank_name|开户银行名称' => 'require',
            'bank_area|开户银行区域' => 'require',
            'bank_address|开户银行具体地址' => 'require',
            'sub_branch_name|支行名称' => 'require',
            'bank_number|银行卡号' => 'require',
            'password|提现密码' => 'require',
        ];
        $validate = new Validate($rule);
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        };

        $uinfo = Db('member')->where('uid', $uid)->find();
        if ($uinfo['psd2'] != $data['password']) {
            $this->apiError('提现密码错误！');
        }

        $sinfo = Db('stores')->where('company_name', $data['company_name'])->whereOr('organization_code', $data['organization_code'])->find();
        if ($sinfo) {
            $this->apiError('该商家已存在！');
        }

        $ndata = [
            'uid' => $uid,
            'company_name' => $data['company_name'],
            'organization_code' => $data['organization_code'],
            'store_mobile' => $data['store_mobile'],
            'store_imgs' => $data['store_imgs'],
            'store_area' => $data['store_area'],
            'store_address' => $data['store_address'],
            'store_type' => $data['store_type'],
            'legal_area' => $data['legal_area'],
            'legal_name' => $data['legal_name'],
            'legal_mobile' => $data['legal_mobile'],
            'business_license_img' => $data['business_license_img'],
            'ID1' => $data['ID1'],
            'ID2' => $data['ID2'],
            'bank_name' => $data['bank_name'],
            'bank_area' => $data['bank_area'],
            'bank_address' => $data['bank_address'],
            'sub_branch_name' => $data['sub_branch_name'],
            'bank_number' => $data['bank_number'],
        ];

        $res = Db('stores')->insert($ndata);
        if ($res) {
            $this->apiSuccess('提交成功，请耐心等待审核！');
        } else {
            $this->apiError('提交失败！');
        }

    }
}